package com.intct.hbase.dml.impl;

import com.intct.hbase.dml.AbsDMLSerivce;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025-09-29 16:41
 *
 * DependentColumnFilter
 * KeyOnlyFilter
 * ColumnCountGetFilter
 * SingleColumnValueFilter                  单值
 * PrefixFilter                             前缀
 * SingleColumnValueExcludeFilter
 * FirstKeyOnlyFilter
 * ColumnRangeFilter
 * ColumnValueFilter                        字段值
 * TimestampsFilter
 * FamilyFilter                             列族
 * QualifierFilter
 * ColumnPrefixFilter
 * RowFilter                                Rowkey
 * MultipleColumnPrefixFilter
 * InclusiveStopFilter
 * PageFilter                               分页
 * ValueFilter                              值
 * ColumnPaginationFilter
 */
public class ScanData extends AbsDMLSerivce {
    @Override
    public void execCommand(Table table) {
        Scan scan = new Scan();

        // 指定版本
//        scan.readVersions(2);

        // 指定rowkey范围 左闭右开
//        scan.withStartRow("1001".getBytes());
//        scan.withStopRow("1002".getBytes(), true);

        FilterList filterList = new FilterList();

        // 过滤
        // 单值过滤
        SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
                // 列族
                "f1".getBytes(),
                // 列限定符
                "name".getBytes(),
                // 比较符
                CompareOperator.EQUAL,
                // 值
                "zhaolu".getBytes()
        );
        singleColumnValueFilter.setFilterIfMissing(true);

        filterList.addFilter(singleColumnValueFilter);

        // rowkey
        RegexStringComparator regexStringComparator = new RegexStringComparator("100.*");
        RowFilter rowFilter = new RowFilter(CompareOperator.EQUAL, regexStringComparator);
        filterList.addFilter(rowFilter);

        scan.setFilter(filterList);

        try {
            // 执行scan检索
            ResultScanner scanner = table.getScanner(scan);

            for (Result result : scanner) {
                Cell[] cells = result.rawCells();
                for (Cell cell : cells) {
                    String rowkey = new String(CellUtil.cloneRow(cell));
                    String family = new String(CellUtil.cloneFamily(cell));
                    String qualifier = new String(CellUtil.cloneQualifier(cell));
                    String value = new String(CellUtil.cloneValue(cell));
                    System.out.println("rowkey : " + rowkey + ";family : " + family + ";qualifier : " + qualifier + ";value : " + value);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        new ScanData().start("intct85", "t3");
    }
}
